package com.example.apione.model;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.apione.dao.ApiInfoDao;
import com.example.apione.entity.ApiInfo;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;

/**
 * @author 尘落
 * @date 2023/2/24
 */
public class addApimodel {
    public static String addapi(ApiInfoDao apiInfoDao, JSONObject jsonObject) {
        ApiInfo apiInfo = new ApiInfo();
        LocalDateTime localDateTime = LocalDateTime.now();
        long creattime = localDateTime.toEpochSecond(ZoneOffset.ofHours(8));

        String redata = jsonObject.getString("request_parameter");
        JSONObject json2 = new JSONObject();
        if (!redata.isEmpty()) {
            String[] m = redata.split("\\+");
            ArrayList<JSONObject> arrayList = new ArrayList<>();
            for (String data1 : m) {
                String[] data2 = data1.split("--");
                JSONObject json = new JSONObject();
                json.put("name", data2[0].substring(1));
                json.put("type", data2[1]);
                json.put("required", data2[2]);
                json.put("info", data2[3]);
                arrayList.add(json);
            }
            json2.put("data", arrayList);
        } else {
            json2.put("data","");

        }


        apiInfo.setCount(0);
        apiInfo.setDoc(jsonObject.getString("doc"));
        apiInfo.setFormat(jsonObject.getString("format"));
        apiInfo.setExampleurl(jsonObject.getString("example_url"));
        apiInfo.setDesc1(jsonObject.getString("desc"));
        apiInfo.setMethod(jsonObject.getString("method"));
        apiInfo.setPath(jsonObject.getString("path"));
        apiInfo.setPhpexample(jsonObject.getString("PHP_example"));
        apiInfo.setRequestparameter(String.valueOf(json2));
        apiInfo.setRetur(jsonObject.getString("return"));
        apiInfo.setStatus(jsonObject.getString("status"));
        apiInfo.setName(jsonObject.getString("name"));
        apiInfo.setCreatetime(creattime);
//        try {
            QueryWrapper<ApiInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("doc", jsonObject.getString("doc"));
            Long count = apiInfoDao.selectCount(queryWrapper);
            if (count > 0) {
                return "已存在";
            } else {
                apiInfoDao.insert(apiInfo);
                return "ok";
            }
//        } catch (Exception e) {
//            return "off";
//        }


    }

    public static String upapi(ApiInfoDao apiInfoDao, JSONObject jsonObject){
        ApiInfo apiInfo =new ApiInfo();
        LocalDateTime localDateTime =LocalDateTime.now();
        long creattime =localDateTime.toEpochSecond(ZoneOffset.ofHours(8));
        JSONObject json2 = new JSONObject();
        String redata=jsonObject.getString("request_parameter");
        if (!redata.isEmpty()) {
        String[] m = redata.split("\\+");
        ArrayList<JSONObject> arrayList =new ArrayList<>();
        for (String data1:m) {
            String[] data2 =data1.split("--");
            JSONObject json=new JSONObject();
            json.put("name",data2[0].substring(1));
            json.put("type",data2[1]);
            json.put("required",data2[2]);
            json.put("info",data2[3]);
            arrayList.add(json);
        }
            json2.put("data", arrayList);
        } else {
            json2.put("data","");

        }
//        apiInfo.setDoc(jsonObject.getString("doc"));
        apiInfo.setId(Integer.valueOf(jsonObject.getString("id")));
        apiInfo.setFormat(jsonObject.getString("format"));
        apiInfo.setExampleurl(jsonObject.getString("example_url"));
        apiInfo.setDesc1(jsonObject.getString("desc"));
        apiInfo.setMethod(jsonObject.getString("method"));
        apiInfo.setPath(jsonObject.getString("path"));
        apiInfo.setPhpexample(jsonObject.getString("PHP_example"));
        apiInfo.setRequestparameter(String.valueOf(json2));
        apiInfo.setRetur(jsonObject.getString("return"));
        apiInfo.setStatus(jsonObject.getString("status"));
        apiInfo.setName(jsonObject.getString("name"));
        apiInfo.setCreatetime(creattime);
        try {
            apiInfoDao.updateById(apiInfo);
            return "ok";
        }catch (Exception e){
            return "off";
        }


    }

    public static String delapi(ApiInfoDao apiInfoDao, JSONObject jsonObject){
        String id=jsonObject.getString("id");
        try {
            apiInfoDao.deleteById(Integer.valueOf(id));
            return "ok";
        }catch (Exception e){
            return "off";
        }

    }

}
